﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Security;
using NotWindowsStore.Models;

namespace NotWindowsStore
{
    public class NotWindowsStoreMembershipProvider : MembershipProvider
    {
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == username);
                if (user != null)
                {
                    //TODO Log.Warn("user '" + username + "' already exists");
                    status = MembershipCreateStatus.DuplicateUserName;
                    return null;
                }

                user = new NwsUser
                {
                    UserName = username,
                    Active = true
                };
                user.SetPassword(password);
                ctx.NwsUsers.AddObject(user);
                ctx.SaveChanges();
                status = MembershipCreateStatus.Success;
                return user.ToMembershipUser();
            }
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    //TODO Log.Warn("Could not find user '" + username + "'");
                    return false;
                }
                if (!user.CheckPassword(oldPassword))
                {
                    //TODO Log.Warn("Old password does not match [username: " + username + "]");
                    return false;
                }
                user.SetPassword(newPassword);
                ctx.SaveChanges();
                return true;
            }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    //TODO Log.Warn("User not found '" + username + "'");
                    return false;
                }
                if (!user.CheckPassword(password))
                {
                    //TODO Log.Warn("User password does not match [username: " + username + "]");
                    return false;
                }
                if (!user.Active)
                {
                    //TODO Log.Warn("User locked out [username: " + username + "]");
                    return false;
                }
                return true;
            }
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    //TODO Log.Warn("User not found '" + username + "'");
                    return null;
                }
                return user.ToMembershipUser();
            }
        }

        public override string GetUserNameByEmail(string email)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == email);
                if (user == null)
                {
                    //TODO Log.Warn("User not found '" + email + "'");
                    return null;
                }
                return user.UserName;
            }
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            using (var ctx = new notwindowsentities())
            {
                var user = ctx.NwsUsers.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    //TODO Log.Warn("User not found '" + username + "'");
                    return false;
                }
                ctx.DeleteObject(user);
                return true;
            }
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override bool EnablePasswordReset
        {
            get { return true; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return true; }
        }

        public override string ApplicationName
        {
            get { return "Not Windows Store"; }
            set { throw new NotImplementedException(); }
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 5; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 5; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return true; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Hashed; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 4; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }
    }
}